package com.yzt.minemodule.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.entity.mine.wallet.WalletBean;
import com.yzt.zhuju.utils.ViewUtil;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.WalletListAdapter;
import com.yzt.minemodule.vm.OrderViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

/**
 * 作者：bobwang
 * 创建时间：2023-06-13 00:16:33
 * 描述：账单列表
 */
@SuppressLint({"NonConstantResourceId", "SetTextI18n"})
public class WalletListActivity extends BaseActivity implements OnRefreshListener {

    private WalletListAdapter adapter;

    private OrderViewModel findViewModel;

    private RefreshLayout mRefreshLayout;

    private int extraId = 1;

    /**
     * @param extraId 1 糖币 2 收益 3 历史
     */
    public static void startSelf(Context activity, int extraId) {
        activity.startActivity(new Intent(activity, WalletListActivity.class)
                .putExtra("extra_id", extraId));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_list;
    }

    @Override
    public void installViews() {
        extraId = getIntent().getIntExtra("extra_id", 0);
        if (extraId == 1) {
            mTitleBar.setTitle("糖币明细");
        } else if (extraId == 2) {
            mTitleBar.setTitle("收益明细");
        } else {
            mTitleBar.setTitle("历史账单");
        }
        findViewModel = new ViewModelProvider(this).get(OrderViewModel.class);
        RecyclerView recyclerView = findViewById(R.id.rv_discover);
        // 单页加载控件
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new WalletListAdapter();
        adapter.setType(extraId);
        recyclerView.setAdapter(adapter);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        addListener();

        findViewModel.getWalletList(extraId, mCurrentPage, PAGE_SIZE);

        findViewModel.friendListSuccessEvent.observe(this, list -> {
            if (list != null && list.size() > 0) {
                if (mCurrentPage == 1) {
                    adapter.removeEmptyView();
                    adapter.setList(list);
                } else {
                    adapter.addData(list);
                }
                adapter.getLoadMoreModule().loadMoreComplete();
            } else {
                if (mCurrentPage == 1) {
                    adapter.setEmptyView(getEmptyView());
                }
                adapter.getLoadMoreModule().loadMoreEnd();
            }
        });

        findViewModel.friendListFinallyEndEvent.observe(this, flag -> {
            if (flag) {
                mRefreshLayout.finishRefresh();
            }
        });
    }

    /**
     * 列表刷新 加载 点击事件
     */
    private void addListener() {
        mRefreshLayout.setOnRefreshListener(this);
        adapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                mCurrentPage++;
                findViewModel.getWalletList(extraId, mCurrentPage, PAGE_SIZE);
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                WalletBean userInfo = adapter.getItem(position);
                if (ViewUtil.isFastDoubleClick()) {
                    return;
                }
                if (userInfo.isShowTitle()) {
                    return;
                }

                if (extraId == 3) {
                    WalletDetailActivity.startSelf(WalletListActivity.this, userInfo.getTransactionsId());
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        findViewModel.getWalletList(extraId, mCurrentPage, PAGE_SIZE);
    }
}